#!/bin/bash
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   runtest.sh of /tools/systemtap/Sanity/byteman-java-methods-probing
#   Description: byteman-java-methods-probing
#   Author: Martin Cermak <mcermak@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   Copyright (c) 2013 Red Hat, Inc. All rights reserved.
#
#   This copyrighted material is made available to anyone wishing
#   to use, modify, copy, or redistribute it subject to the terms
#   and conditions of the GNU General Public License version 2.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE. See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1

export DEBUGINFOD_URLS="https://debuginfod.fedoraproject.org/ $DEBUGINFOD_URLS"
export DEBUGINFOD_TIMEOUT=300

rlJournalStart
    rlPhaseStartSetup
        # At this point we simply rely on that the %triggerin scriptlet from
        # systemtap-runtime-java did it's job well.  There was a hack to force
        # trigger it here, but commit respective to this comment removes it.
        # https://bugzilla.redhat.com/show_bug.cgi?id=1732514#c11

        rlRun "rpm -qa | fgrep -e systemtap-runtime-java -e byteman -e openjdk | sort"

        rlRun "stap-prep"

        for BINARY in java javac; do
            rlRun "alternatives --display $BINARY | grep -v slave"
            rlRun "$BINARY -version"
        done

        rlRun "TMPDIR=$( mktemp -d )"
        rlRun "mkdir -p $TMPDIR/foo/bar"
        rlRun "cp ThreadedExample.java $TMPDIR/foo/bar/"
        rlRun "cp ThreadedExample.stp $TMPDIR/"
        rlRun "pushd $TMPDIR"
    rlPhaseEnd

    rlPhaseStart FAIL 'Prepare bytecode class'
        rlRun "javac foo/bar/ThreadedExample.java"
    rlPhaseEnd

    rlPhaseStart FAIL 'Prepare the LKML'
        rlRun "stap -p4 -m ThreadedExample ThreadedExample.stp"
    rlPhaseEnd

    rlPhaseStart FAIL 'Check the Feature'
        java foo.bar.ThreadedExample &
        rlRun "jps -l | grep 'foo.bar.ThreadedExample'"
        export STAPBM_VERBOSE=yes
        # rlRun "stap --poison-cache -vv ThreadedExample.stp 2>&1 | tee testout.log"
        # Speed the stap startup up by pre-compiling the module within separate
        # phase 'Prepare the LKML' above.
        rlRun "staprun -v -R ThreadedExample.ko 2>&1 | tee testout.log"
        rlRun "grep -q 'Hit printMessage(int): 42' testout.log"
        rlRun "grep -q 'Hit printMessage(long): 987234864' testout.log"
    rlPhaseEnd

    rlPhaseStartCleanup
        rlRun "popd"
        rlRun "rm -r $TMPDIR"
    rlPhaseEnd
rlJournalPrintText
rlJournalEnd
